//
//  outMoneyViewController.m
//  WZEfengAndEtong
//
//  Created by 李旭 on 16/9/23.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "outMoneyViewController.h"
#import "Masonry.h"
#import "CXmppRequestMgr.h"
#import "MBProgressHUD.h"
#import "CContactModel.h"
#import "CContactMgr.h"
#import "PopPasswordView.h"
@interface outMoneyViewController ()<PopPasswordViewDelegate>
@property(nonatomic,weak)UILabel *blanceLable;
@property(nonatomic,weak)UITextField *moneyTextField;
@property(nonatomic,weak)UITextField *pwdTextField;
@property(nonatomic,weak)UIButton *outButton;
@property(nonatomic,strong)CContactModel *currentUser;
@property(nonatomic,strong) PopPasswordView *passwordView;
@end

@implementation outMoneyViewController
-(CContactModel *)currentUser
{
    if (_currentUser == nil) {
        _currentUser = [[[CContactMgr alloc]init] loadCurContact];
    }
    return _currentUser;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setNewTitleCenter:@"提现"];
    self.view.backgroundColor = RGBCOLOR(243, 243, 243);
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self configInterFace];
}
-(void)configInterFace
{
    UILabel *headLabel = [[UILabel alloc]init];
    headLabel.text = @"您当前的余额";
    headLabel.font = [UIFont systemFontOfSize:13];
    headLabel.textColor = RGBCOLORWITH16COLOR(0x999999);
    headLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:headLabel];
    [headLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.width.mas_equalTo(100);
        make.top.mas_equalTo(10);
        make.height.mas_equalTo(30);
    }];
    UILabel *blanceLabel = [[UILabel alloc]init];
    blanceLabel.text =self.blance;
    blanceLabel.font = [UIFont systemFontOfSize:13];
    blanceLabel.textColor = [UIColor redColor];
    blanceLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:blanceLabel];
    self.blanceLable = blanceLabel;
    [blanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(headLabel.mas_right).offset(0);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(30);
        make.top.mas_equalTo(10);
    }];
    UILabel *unitLable = [[UILabel alloc]init];
    unitLable.text = @"元";
    unitLable.font = [UIFont systemFontOfSize:13];
    unitLable.textColor = RGBCOLORWITH16COLOR(0x999999);
    unitLable.textAlignment = NSTextAlignmentRight;
    [self.view addSubview:unitLable];
    [unitLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.equalTo(blanceLabel.mas_right).offset(0);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(30);
    }];
    UIView *moneyView =[[UIView alloc]init];
    moneyView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:moneyView];
    [moneyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(blanceLabel.mas_bottom).offset(0);
        make.right.left.mas_equalTo(0);
        make.height.mas_equalTo(60);
    }];
    UIView *line0 = [[UIView alloc]init];
    line0.backgroundColor = RGBCOLORWITH16COLOR(0x999999);
    [moneyView addSubview:line0];
    [line0 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
    }];

    UIView *line = [[UIView alloc]init];
    line.backgroundColor = RGBCOLORWITH16COLOR(0x999999);
    [moneyView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
    }];
    UITextField *moneyTextField = [[UITextField alloc]init];
    moneyTextField.placeholder = @"请输入提现金额";
    [moneyTextField addTarget:self action:@selector(checkmoney:) forControlEvents:UIControlEventEditingChanged];
    moneyTextField.keyboardType = UIKeyboardTypeDecimalPad;
    moneyTextField.autocorrectionType = UITextAutocorrectionTypeNo;
    self.moneyTextField = moneyTextField;
    [moneyView addSubview:moneyTextField];
    [moneyTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(300);
        make.left.mas_equalTo(15);
    }];
    UILabel *yuanLabel = [[UILabel alloc]init];
    yuanLabel.text = @"元";
    yuanLabel.textColor = [UIColor blackColor];
    yuanLabel.textAlignment = NSTextAlignmentRight;
    [moneyView addSubview:yuanLabel];
    [yuanLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);
        make.right.mas_equalTo(-10);
        make.width.mas_equalTo(40);
    }];
    UILabel *noticelabel = [[UILabel alloc]init];
    noticelabel.font = [UIFont systemFontOfSize:14];
    noticelabel.textColor =  RGBCOLORWITH16COLOR(0x999999);
    noticelabel.text = @"取现最低额度为1元";
    noticelabel.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:noticelabel];
    [noticelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(0);
        make.top.equalTo(moneyView.mas_bottom).offset(0);
        make.height.mas_equalTo(40);
    }];
    UIButton *outButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [outButton setBackgroundColor:RGBCOLOR(253, 156, 65)];
    [outButton setTitle:@"提现" forState:UIControlStateNormal];
    [outButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    outButton.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [outButton addTarget:self action:@selector(outMoney) forControlEvents:UIControlEventTouchUpInside];
    outButton.enabled = NO;
    outButton.layer.cornerRadius = 5;
    outButton.layer.masksToBounds = YES;
    self.outButton = outButton;
    [self.view addSubview:outButton];
    [outButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-30);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(self.view.width - 40);
        make.centerX.mas_equalTo(self.view);
    }];
}
-(void)checkmoney:(UITextField*)textField
{
    if ([self.moneyTextField.text doubleValue] > [self.blanceLable.text doubleValue]) {
        self.moneyTextField.textColor = [UIColor redColor];
        [MBProgressHUD showError:@"余额不足"];
        self.outButton.enabled = NO;
    }
    else
    {
        self.moneyTextField.textColor = [UIColor blackColor];
        self.outButton.enabled = YES;
    }
}
-(void)outMoney
{
    self.passwordView=[[PopPasswordView alloc]initViewwithtype:@"open" andMoney:@"0"];
    self.passwordView.delegate=self;
    self.passwordView.type=@"open";
    [[UIApplication sharedApplication].keyWindow addSubview:self.passwordView];
   
}
-(void)useStoreCode:(NSString *)code
{
    __block MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"正在取现";
    [self.passwordView removeFromSuperview];
    [[CXmppRequestMgr shareInstance]verificationPayPasswordWithUserId:self.currentUser.UserID AndPassWord:code successHandel:^(AFHTTPRequestOperation *operation, id responseObject) {
        if ([responseObject[@"result"] isEqualToString:@"success"]) {
         dispatch_async(dispatch_get_main_queue(), ^{
             [[CXmppRequestMgr shareInstance] getMoneyFromServerWithUsedId:self.currentUser.UserID AndMoney:[self.moneyTextField.text doubleValue] successHandel:^(AFHTTPRequestOperation *operation, id responseObject) {
                 [hud hide:YES];
                 if ([responseObject[@"result"] isEqualToString:@"success"]) {
                     [MBProgressHUD showSuccess:@"取现成功"];
                     [self.navigationController popViewControllerAnimated:YES];
                 }
                 else
                 {
                     [MBProgressHUD showError:@"提现失败"];
                 }
             } filarueHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
                 [hud hide:YES];
                 [MBProgressHUD showError:@"服务器异常"];
             }];

         });
        }
        else
        {
            [hud hide:YES];
            [MBProgressHUD showError:@"密码错误请重试"];
        }
        
    } filarueHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
        [hud hide:YES];
        [MBProgressHUD showError:@"服务器异常"];
    }];
    
    
}
-(void)disAction
{
     [self.passwordView removeFromSuperview];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
@end
